package com.orange.core.util.function;

import java.util.function.Supplier;

@FunctionalInterface
public interface ThrowingSupplier<R, E extends Exception> {

    static <R> Supplier<R> wrapper(ThrowingSupplier<R, Exception> throwingSupplier) {
        return () -> {
            try {
                return throwingSupplier.get();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
    }

    static <R, E extends RuntimeException> Supplier<R> wrapper(ThrowingSupplier<R, Exception> throwingSupplier, E ex) {
        return () -> {
            try {
                return throwingSupplier.get();
            } catch (Exception e) {
                ex.initCause(e);
                throw ex;
            }
        };
    }

    R get() throws E;
}
